<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="询价单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入询价单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有效期" prop="vaild">
        <el-date-picker clearable
          v-model="queryParams.vaild"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择有效期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="询价客户" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择询价客户">
          <el-option
            v-for="dict in khUserList"
            :key="dict.userId"
            :label="dict.nickName"
            :value="dict.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态">
          <el-option
            v-for="dict in dict.type.wms_inquire_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:inquireOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:inquireOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['wms:inquireOrder:add']"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inquireOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :selectable="handelIsSelect"/>
      <el-table-column label="订单编号" align="center" prop="orderCode" >
        <template slot-scope="scope">
          <el-link type="primary" @click="handleLook(scope.row)">{{ scope.row.orderCode }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="vaild" width="180"/>
      <el-table-column label="询价客户" align="center" prop="nickName" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_inquire_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="询价时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.status === '0' || scope.row.status === '4'"
            v-hasPermi="['wms:inquireOrder:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEnd(scope.row)"
            v-hasPermi="['wms:inquireOrder:edit']"
            v-if="scope.row.status === '0' || scope.row.status === '1' || scope.row.status === '2'"
          >结束询价</el-button>
          <el-button
            plain
            icon="el-icon-download"
            size="mini"
            type="text"
            v-if="scope.row.status === '0' || scope.row.status === '1'"
            @click="handleExport(scope.row)"
            v-hasPermi="['wms:inquireOrder:export']"
          >导出</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.status === '0'"
            v-hasPermi="['wms:inquireOrder:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改询价单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编码" v-if="form.orderCode">
          {{form.orderCode}}
        </el-form-item>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="有效期" prop="vaild">
              <el-date-picker clearable
                v-model="form.vaild"
                :picker-options="pickerOptions"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择有效期" style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="询价客户" prop="userId">
              <el-select v-model="form.userId" placeholder="请选择询价客户">
                <el-option
                  v-for="dict in khUserList"
                  :key="dict.userId"
                  :label="dict.nickName"
                  :value="dict.userId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="附件" prop="accessoryList">
          <el-upload
            class="upload-demo"
            :headers="upload.headers"
            :action="upload.picUrl"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            multiple
            accept=".jpg,.png,.jpeg,.pdf"
            :file-list="fileList"
            drag
            style="width: 100%">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip text-center" slot="tip">
              <span>仅允许导入jpg/png/pdf格式文件。</span>
            </div>
          </el-upload>
        </el-form-item>
        <h1>订单商品</h1>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleOrderProductAdd"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleSelectedProduct"
              >选择商品</el-button>
            </el-col>
          </el-row>
          <el-table :data="products" id="alterTable">
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-form label-position="left" inline class="demo-table-expand" label-width="80px">
                  <el-row :gutter="10">
                    <el-col :span="12">
                      <el-form-item label="商品类型">
                        <treeselect v-model="props.row.productType" :options="productTypeOptions" :show-count="true"
                                    placeholder="请选商品类型" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="尺寸单位">
                        <el-select v-model="props.row.ccUnit" placeholder="请选择尺寸单位" clearable>
                          <el-option
                            v-for="dict in dict.type.wms_product_cc_unit"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                          ></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="10">
                    <el-col :span="12">
                      <el-form-item label="长度">
                        <el-input v-model="props.row.longWith" :maxlength="50" placeholder="请输入商品长度" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="宽度">
                        <el-input v-model="props.row.weight" :maxlength="50" placeholder="请输入商品宽度" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="10">
                    <el-col :span="12">
                      <el-form-item label="高度">
                        <el-input v-model="props.row.height" :maxlength="50" placeholder="请输入商品高度" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="重量">
                        <el-input v-model="props.row.zl" :maxlength="50" placeholder="请输入商品重量" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-form-item label="详情">
                    <el-input type="textarea" v-model="props.row.detail" placeholder="请输入详情" />
                  </el-form-item>
                </el-form>
              </template>
            </el-table-column>
            <el-table-column label="商品编码" header-align="center">
              <template slot-scope="scope">
                <el-input v-model="scope.row.productCode" placeholder="请输入商品编码"/>
              </template>
            </el-table-column>
            <el-table-column label="商品名称" header-align="center">
              <template slot-scope="scope">
                <el-input v-model="scope.row.productName" :maxlength="50" placeholder="请输入商品名称" />
              </template>
            </el-table-column>
            <el-table-column label="商品型号" header-align="center">
              <template slot-scope="scope">
                <el-input v-model="scope.row.modal" :maxlength="50" placeholder="请输入商品型号" />
              </template>
            </el-table-column>
            <el-table-column label="指定厂家" header-align="center">
              <template slot-scope="scope">
                <el-select v-model="scope.row.purchaseCompany" placeholder="请选择厂家" clearable filterable>
                  <el-option
                    v-for="item in entOptions"
                    :key="item.id"
                    :label="item.entName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="计数单位" header-align="center">
              <template slot-scope="scope">
                <el-select v-model="scope.row.unit" placeholder="请选择计数单位" clearable>
                  <el-option
                    v-for="unit in unitArr"
                    :key="unit.id"
                    :label="unit.name"
                    :value="unit.id"
                  ></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="商品数量" header-align="center">
              <template slot-scope="scope">
                <el-input v-model="scope.row.productNum" placeholder="请输入商品数量"/>
              </template>
            </el-table-column>
            <el-table-column label="操作" header-align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleOrderProductDelete(scope.$index)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-loading="btnLoading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 结束询价单对话框 -->
    <el-dialog title="结束询价单" :visible.sync="openEnd" width="500px" append-to-body>
      <el-form ref="formEnd" :model="form" :rules="rulesEnd" label-width="80px">
        <el-form-item label="结束原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入结束原因"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEndForm" v-loading="btnLoading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 询价单商品对话框 -->
    <el-dialog title="询价单详情" :visible.sync="openLook" width="80%" append-to-body>
      <el-form>
        <el-form-item label="询价单号" >
          {{orderCode}}
        </el-form-item>

        <el-form-item label="附件">
          <el-upload
            action=""
            class="upload-demo"
            :disabled="true"
            :on-preview="handlePreview"
            :file-list="fileList">
          </el-upload>
        </el-form-item>
      </el-form>
      <h1>商品信息</h1>
      <el-table :data="orderProductTable">
        <el-table-column label="序号" align="center" type="index" width="50"/>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="商品编码">
                    <span>{{ props.row.productCode }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="商品类型">
                    <span>{{ props.row.productTypeName }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="价格">
                    <span>{{ props.row.price }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="尺寸单位">
                    <span>{{ props.row.ccUnitName }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="尺寸">
                    <span v-if="props.row.longWith || props.row.weight || props.row.height">{{ props.row.longWith + '*' + props.row.weight + '*' + props.row.height }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="重量">
                    <span>{{ props.row.zl }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="详情">
                <span>{{ props.row.detail }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" prop="productName" :show-overflow-tooltip="true"/>
        <el-table-column label="商品型号" align="center" prop="modal" :show-overflow-tooltip="true"/>
        <el-table-column label="指定厂家" align="center" prop="purchaseCompanyName"/>
        <el-table-column label="商品数量" align="center" prop="productNum"/>
        <el-table-column label="计数单位" align="center" prop="unitName"/>
      </el-table>
    </el-dialog>

    <!-- 选择商品对话框 -->
    <el-dialog title="选择商品" :visible.sync="openSelectedProduct" fullscreen append-to-body>
      <selected-product ref="selectedProduct" :filter-keys="filterKeys" :open-flag="openSelectedProduct"></selected-product>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSelectedProduct">确 定</el-button>
        <el-button @click="openSelectedProduct = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 询价单导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="600px" append-to-body>
      <el-form ref="formImport" :model="upload.form" :rules="upload.rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="有效期" prop="vaild">
              <el-date-picker clearable
                v-model="upload.form.vaild"
                type="date"
                :picker-options="pickerOptions"
                value-format="yyyy-MM-dd"
                style="width: 100%"
                placeholder="请选择有效期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="询价客户" prop="userId">
              <el-select v-model="upload.form.userId" placeholder="请选择询价客户">
                <el-option
                  v-for="dict in khUserList"
                  :key="dict.userId"
                  :label="dict.nickName"
                  :value="dict.userId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="附件" prop="accessoryList">
          <el-upload
            class="upload-demo"
            :headers="upload.headers"
            :action="upload.picUrl"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            multiple
            accept=".jpg,.png,.jpeg,.pdf"
            :file-list="fileList"
            drag
            style="width: 100%">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip text-center" slot="tip">
              <span>仅允许导入jpg/png/pdf格式文件。</span>
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="导入内容">
          <template slot="label">
            导入内容
            <el-tooltip class="item" effect="dark" content="导入的五种模板：
商品名称 型号 数量 指定厂家
商品名称 型号 数量
商品名称 型号 数量 指定厂家 毫米/厘米/分米/米 长*宽*高 重量 备注
商品名称 型号 数量 毫米/厘米/分米/米 长*宽*高 重量 备注
商品名称 型号 数量 指定厂家 毫米/厘米/分米/米 长*宽*高 重量
商品名称 型号 数量 毫米/厘米/分米/米 长*宽*高 重量
商品编码 数量" placement="bottom">
              <i class="el-icon-warning-outline"></i>
            </el-tooltip>
          </template>
          <el-input
            type="textarea"
            :rows="10"
            placeholder="导入的五种模板：
商品名称 型号 数量 指定厂家
商品名称 型号 数量
商品名称 型号 数量 指定厂家 毫米/厘米/分米/米 长*宽*高 重量 备注
商品名称 型号 数量 毫米/厘米/分米/米 长*宽*高 重量 备注
商品名称 型号 数量 指定厂家 毫米/厘米/分米/米 长*宽*高 重量
商品名称 型号 数量 毫米/厘米/分米/米 长*宽*高 重量
商品编码 数量"
            v-model="upload.form.importContent">
          </el-input>
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="cancelImport">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInquireOrder,
  getInquireOrder,
  delInquireOrder,
  addInquireOrder,
  updateInquireOrder,
  endInquireOrder,
  getOrderProduct, importInquireOrder
} from "@/api/wms/inquireOrder";
import { listUserKhXlk } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listUnitXlk} from "@/api/wms/unit";
import { getToken } from "@/utils/auth";
import selectedProduct from "@/views/wms/inquireOrder/components/selectedProduct.vue";
import {listProductTypeTree} from "@/api/wms/productType";
import {listEntXlk} from "@/api/wms/ent";
import { parseTime } from '@/utils/ruoyi'

export default {
  name: "InquireOrder",
  components: {Treeselect,selectedProduct},
  dicts: ['wms_inquire_order_status','wms_product_cc_unit'],
  data() {
    return {
      prefix: process.env.VUE_APP_BASE_API,
      //按钮加载
      btnLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数据
      rows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 询价单管理表格数据
      inquireOrderList: [],
      // 客户下拉框数据
      khUserList: [],
      // 商品类型列表
      productTypeOptions: [],
      // 订单商品
      orderProductTable: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 结束询价单弹出层
      openEnd: false,
      // 查看订单商品弹出层
      openLook: false,
      // 选择商品弹出层
      openSelectedProduct: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        vaild: null,
        userId: null,
        status: null
      },
      // 询价单导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        picUrl: process.env.VUE_APP_BASE_API + "/wms/inquireOrder/uploadPic",
        form: {
          vaild: null,
          userId: null,
          importContent: null,
          accessoryList: null
        },
        rules:{
          vaild: [
            { required: true, message: "请选择有效期", trigger: "blur" }
          ],
          userId: [
            { required: true, message: "请选择客户", trigger: "blur" }
          ],
          importContent: [
            { required: true, message: "导入内容不能为空", trigger: "blur" }
          ],
          accessoryList: [
            { required: true, message: "附件不可为空", trigger: "blur" }
          ]
        }
      },
      fileList: [],
      accessoryList: [],
      // 表单商品
      products: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        vaild: [
          { required: true, message: "请选择有效期", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "请选择客户", trigger: "blur" }
        ],
        accessoryList: [
          { required: true, message: "附件不可为空", trigger: "blur" }
        ]
      },
      unitArr: [],
      // 表单校验
      rulesEnd: {
        reason: [
          { required: true, message: "结束原因不能为空", trigger: "blur" },
          { max: 255, message: '结束原因长度最多 255个字', trigger: 'blur' }
        ]
      },
      filterKeys: [],
      // 供应商集合
      entOptions: [],
      pickerOptions: {
        disabledDate(time) {
          //时间选择框必须大于今天
          return time.getTime() < Date.now() - 8.64e7;
          // 或者 return time.getTime() < Date.now();
        }
      },
      orderCode: null
    };
  },
  created() {
    this.getList();
    this.getKhUserList();
    this.getProductType();
    this.getEntList();
    this.getUnitList();
  },
  methods: {
    /** 查询询价单管理列表 */
    getList() {
      this.loading = true;
      listInquireOrder(this.queryParams).then(response => {
        this.inquireOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询客户下拉框 */
    getKhUserList() {
      listUserKhXlk().then(response => {
        this.khUserList = response.data;
      });
    },
    /** 查询商品类型 */
    getProductType(){
      listProductTypeTree().then(res => {
        this.productTypeOptions = res.data;
      })
    },
    /** 查询供应商列表 */
    getEntList() {
      listEntXlk({entType: '2', gysType: '1'}).then(response => {
        this.entOptions = response.data;
      });
    },
    //获取商品单位
    getUnitList(){
      listUnitXlk().then(res => {
        this.unitArr = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openEnd = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.accessoryList = []
      this.fileList = []
      this.form = {
        orderCode: null,
        vaild: null,
        price: null,
        userId: null,
        reason: null,
        accessoryList: null
      };
      this.products = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderCode)
      this.rows = selection
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加询价单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderCode = row.orderCode || this.ids
      getInquireOrder(orderCode).then(async response => {
        this.form = response.data;
        this.accessoryList = this.form.accessoryList
        if (this.accessoryList && this.accessoryList.length > 0) {
          this.accessoryList.forEach(item => {
            let urls = item.split("/")
            this.fileList.push({name: urls[urls.length - 1], url: this.prefix + item})
          })
        } else {
          this.accessoryList = []
        }
        this.products = this.form.products
        this.open = true;
        this.title = "修改询价单管理";
      });
    },
    /** 结束询价按钮操作 */
    handleEnd(row) {
      this.reset();
      this.form.userId = row.userId;
      this.form.orderCode = row.orderCode;
      this.form.createTime = row.createTime;
      this.form.status = row.status;
      this.openEnd = true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let msg = ''
          if (this.products.length > 0) {
            let regNum = /^(0|[1-9])\d{0,6}(\.\d{0,2})?$/
            let reg = /^(0|[1-9])\d{0,6}(\.\d{0,2})?$/
            for (let i = 0; i < this.products.length; i++) {
              let item = this.products[i];
              if (!item.productName) {
                msg += "第" + (i+1) +"行格式异常，商品名称不可为空；"
              } else if (item.productName.length > 50) {
                msg += "第" + (i+1) +"行格式异常，商品名称最大长度50个字；"
              }
              if (!item.modal) {
                msg += "第" + (i+1) +"行格式异常，商品型号不可为空；"
              } else if (item.modal && item.modal.length > 50) {
                msg += "第" + (i+1) +"行格式异常，商品型号最大长度50个字；"
              }
              if (item.detail && item.detail.length > 255) {
                msg += "第" + (i+1) +"行格式异常，商品详情最大长度255个字；"
              }
              if (item.productNum) {
                if (!regNum.test(item.productNum)) {
                  msg += "第" + (i+1) +"行格式异常，商品数量只能是整数最大7位整数2位小数；"
                }
              } else {
                msg += "第" + (i+1) +"行格式异常，商品数量不能为空；"
              }
              if (item.longWith) {
                if (!reg.test(item.longWith)) {
                  msg += "第" + (i+1) +"行格式异常，商品长度只能是数值最大7位整数2位小数；"
                }
              }
              if (item.weight) {
                if (!reg.test(item.weight)) {
                  msg += "第" + (i+1) +"行格式异常，商品宽度只能是数值最大7位整数2位小数；"
                }
              }
              if (item.height) {
                if (!reg.test(item.height)) {
                  msg += "第" + (i+1) +"行格式异常，商品高度只能是数值最大7位整数2位小数；"
                }
              }
              if (item.zl) {
                if (!reg.test(item.zl)) {
                  msg += "第" + (i+1) +"行格式异常，商品重量只能是数值最大7位整数2位小数；"
                }
              }
            }

            if (msg) {
              this.$message.error(msg)
            } else {
              this.btnLoading = true
              this.form.accessoryList = this.accessoryList
              this.form.products = this.products
              if (this.form.orderCode != null) {
                updateInquireOrder(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }).finally(f => {
                  this.btnLoading = false
                });
              } else {
                addInquireOrder(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }).finally(f => {
                  this.btnLoading = false
                });
              }
            }
          } else {
            this.$message.error("商品信息不可为空！")
          }
        }
      });
    },
    /** 提交结束按钮 */
    submitEndForm() {
      this.$refs["formEnd"].validate(valid => {
        if (valid) {
          this.btnLoading = true
          endInquireOrder(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.openEnd = false;
            this.getList();
          }).finally(f => {
            this.btnLoading = false
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderCodes = row.orderCode || this.ids;
      this.$modal.confirm('是否确认删除询价单管理编号为"' + orderCodes + '"的数据项？').then(function() {
        return delInquireOrder(orderCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport(row) {
      this.download('wms/inquireOrder/export?orderCode=' + row.orderCode, {}, `询价单_${row.orderCode}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "询价单导入";
      this.upload.form.vaild = null
      this.upload.form.userId = null
      this.upload.form.importContent = null
      this.upload.form.accessoryList = null
      this.accessoryList = []
      this.fileList = []
      this.upload.open = true;
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs["formImport"].validate(valid => {
        if (valid) {
          this.upload.form.accessoryList = this.accessoryList
          importInquireOrder(this.upload.form).then(async response => {
            if (response.code == 200) {
              this.upload.open = false;
              this.getList()
              this.$modal.msgSuccess("导入成功");
            } else {
              this.$modal.msgError(response.msg);
            }
          });
        }
      })
    },
    cancelImport() {
      this.upload.open = false
      this.upload.form.vaild = null
      this.upload.form.userId = null
    },
    // 查看订单商品
    handleLook(row){
      this.orderProductTable = []
      this.fileList = []
      this.orderCode = row.orderCode;
      getInquireOrder(row.orderCode).then(response => {
        let data = response.data;
        this.openLook = true
        this.orderProductTable = data.products
        let accessoryList = data.accessoryList
        if (accessoryList && accessoryList.length > 0) {
          accessoryList.forEach(item => {
            let urls = item.split("/")
            this.fileList.push({name: urls[urls.length - 1], url: this.prefix + item})
          })
        }
      })
    },
    // 删除订单商品
    handleOrderProductDelete(index){
      this.products.splice(index, 1);
    },
    // 新增订单商品
    handleOrderProductAdd(){
      this.products.push({
        productCode: null,
        productName: null,
        shortName: null,
        productNum: 0,
        ccUnit: null,
        longWith: 0,
        weight: 0,
        height: 0,
        unit: null,
        productType: null,
        zl: 0,
        modal: null
      })
    },
    // 选择商品对话框
    handleSelectedProduct(){
      if(this.form.products) {
        this.filterKeys = this.form.products.map(e => e.productCode)
      } else {
        this.filterKeys = []
      }

      this.openSelectedProduct = true
    },
    // 提交选择的商品
    async submitSelectedProduct(){
      let selectedArr = this.$refs.selectedProduct.getSelectedProductList()
      if (selectedArr.length > 0) {
        for (let i = 0; i < selectedArr.length; i++) {
          let item = selectedArr[i]

          this.products.push({
            productCode: item.productCode,
            productName: item.productName,
            shortName: item.shortName,
            productNum: item.productNum,
            ccUnit: item.ccUnit,
            longWith: item.longWith,
            weight: item.weight,
            height: item.height,
            unit: item.unit,
            productType: item.productType,
            zl: item.zl,
            modal: item.modal
          })
        }
        this.openSelectedProduct = false
      } else {
        this.$message.error("请选择商品！")
      }
    },
    // 是否可以选择
    handelIsSelect(row, index){
      return row.status === '0'
    },
    handleRemove(file, fileList) {
      this.accessoryList.splice(this.accessoryList.indexOf(file.url), 1);
      if (this.accessoryList.length === 0) {
        this.form.accessoryList = null
        this.upload.form.accessoryList = null
      }
    },
    // 附件上传成功处理
    handleSuccess(response, file, fileList) {
      if (response.code == 200) {
        this.form.accessoryList = '123'
        this.upload.form.accessoryList = '123'
        this.accessoryList.push(response.imgUrl)
      }
    },
    // 文件点击事件
    handlePreview(file){
      window.open(file.url,'_blank');
    },
  }
};
</script>
<style scoped>
/deep/ .el-form-item--medium .el-form-item__content{
  line-height: 32px !important;
}
/deep/ .el-select{
  width: 100%;
}
/deep/ .el-upload-dragger{
  width: 100%;
}
/deep/ .el-upload{
  width: 100%;
}
/deep/ .el-table__body-wrapper{
  overflow: visible !important;
}
/deep/ .el-table .cell{
  overflow: visible;
}
/deep/ .el-table{
  overflow: visible !important;
}
/deep/ .el-table .cell.el-tooltip{
  overflow: hidden !important;
}
</style>
